package com.xxwu.springgobang.component;

import com.xxwu.springgobang.model.Room;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class RoomManager {
    //创建房间管理器
    HashMap<String, Room> rooms = new HashMap<>();
    //还需要建立 user 和 room 之间的联系
    HashMap<Integer, String> userIdToRoomId = new HashMap<>();

    public void add(Room room, int userId1, int userId2) {
        rooms.put(room.getRoomId(), room);
        userIdToRoomId.put(userId1, room.getRoomId());
        userIdToRoomId.put(userId2, room.getRoomId());
    }
    public void remove(String roomId, int userId1, int userId2) {
        rooms.remove(roomId);
        userIdToRoomId.remove(userId1);
        userIdToRoomId.remove(userId2);
    }

    public Room getRoomByRoomId(String roomId) {
        return rooms.get(roomId);
    }

    public Room getRoomByUserId(int userId) {
        String roomId = userIdToRoomId.get(userId);
        if(roomId == null) {
            return null;
        }
        return rooms.get(roomId);
    }
}
